package org.finmanager.core;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by Kelvin on 1/4/2014.
 */

//Calling different UI and models
public class ItemManagerController implements ActionListener {
	//Some constants
	public static final String ADD_ITEM = "ADD_ITEM";
	public static final String EDIT_ITEM = "EDIT_ITEM";
	public static final String DELETE_ITEM = "DELETE_ITEM";

	public static final String OPEN_ITEM_INPUT_UI_ADD = "OPEN_ITEM_INPUT_UI_ADD";
	public static final String OPEN_ITEM_INPUT_UI_ADD_CATEGORY = "OPEN_ITEM_INPUT_UI_ADD_CATEGORY";
	public static final String OPEN_ITEM_INPUT_UI_EDIT = "OPEN_ITEM_INPUT_UI_EDIT";

	public static final String OPEN_CATEGORY_MANAGER = "OPEN_CATEGORY_MANAGER";

	public static final String OPEN_ITEM_FILTER_UI = "OPEN_ITEM_FILTER_UI";

	public static final String APPLY_FILTER = "APPLY_FILTER";
	public static final String RESET_FILTER = "RESET_FILTER";


	private AbstractItemInputUI itemInputUI;

	private CategoryManagerController categoryManagerController;

	private final FinManager finManager;
	private final ItemModel itemModel;
	private final CategoryModel categoryModel;
	private final ItemManagerUI itemManagerUI;

	private final FavouritesPanelUI favouritesPanelUI;

	public ItemManagerController(FinManager finManager) {
		this.finManager = finManager;
		this.itemModel = finManager.getItemModel();
		this.categoryModel = finManager.getCategoryModel();
		this.itemManagerUI = new ItemManagerUI(this);

		itemManagerUI.addButtonListener(this);

		favouritesPanelUI = new FavouritesPanelUI(itemManagerUI.getFavouritesPanel(), this);

		categoryManagerController = new CategoryManagerController(finManager, this);
	}

	public ItemModel getItemModel() {
		return itemModel;
	}

	public JComponent getUIMainComponent() {
		return itemManagerUI.getMainComponent();
	}

	public FavouritesPanelUI getFavouritesPanelUI() {
		return favouritesPanelUI;
	}

	//defining some options to be done when receiving command
	@Override
	public void actionPerformed(ActionEvent e) {
		Item item;
		int row;

		String ac = e.getActionCommand();

		if (ac.equals(ADD_ITEM))
		{
			item = itemInputUI.getItemFromInput();
			itemModel.addItem(item);
		}
		else if (ac.equals(EDIT_ITEM))
		{
			item = itemInputUI.getItemFromInput();
			row = itemManagerUI.getSelectedRowInModel();
			itemModel.editItem(row, item);
		}
		else if (ac.equals(DELETE_ITEM))
		{
			row = itemManagerUI.getSelectedRowInModel();
			itemModel.deleteItem(row);
		}
		else if (ac.equals(OPEN_ITEM_INPUT_UI_ADD))
		{
			itemInputUI = new ItemInputUIAdd(this, categoryModel.getCategoryComboBoxModel());
		}
		else if (ac.equals(OPEN_ITEM_INPUT_UI_ADD_CATEGORY))
		{
			Category category;
			JButton button = (JButton) e.getSource();
			category = finManager.getCategoryList().findCategory(button.getText());
			itemInputUI = new ItemInputUIAdd(this, categoryModel.getCategoryComboBoxModel(), category);
		}
		else if (ac.equals(OPEN_ITEM_INPUT_UI_EDIT))
		{
			item = finManager.getItemList().getItem(itemManagerUI.getSelectedRowInModel());
			itemInputUI = new ItemInputUIEdit(this, categoryModel.getCategoryComboBoxModel(), item);
		}
		else if (ac.equals(OPEN_CATEGORY_MANAGER))
		{
			categoryManagerController.actionPerformed(e);
		}
		else if (ac.equals(OPEN_ITEM_FILTER_UI))
		{
			new ItemFilterUI(categoryManagerController, this);
		}
		else if (ac.equals(APPLY_FILTER))
		{
			itemManagerUI.setFilter(categoryModel.getCategoryTableModel().getFilter());
		}
		else if (ac.equals(RESET_FILTER))
		{
			categoryModel.getCategoryTableModel().resetFilter();
			itemManagerUI.setFilter(null);
		}
	}
}
